//
//  LoginViewController.m
//  BobNews
//
//  Created by lanou3g on 16/5/5.
//  Copyright © 2016年 YJC. All rights reserved.
//

#import "LoginViewController.h"
#import "RegistViewController.h"
#import "NetWorkRequestManager.h"
#import "UserInfoManager.h"
#import "ToutiaoTableViewController.h"
@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (nonatomic,strong) NSMutableArray *loginArray;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"a.jpeg"]];
    UIBarButtonItem *registBtn = [[UIBarButtonItem alloc]initWithTitle:@"注册" style:UIBarButtonItemStylePlain target:self action:@selector(registBtnAction)];
    
    self.navigationItem.rightBarButtonItem = registBtn;
    // Do any additional setup after loading the view from its nib.
}
- (void)registBtnAction {
    RegistViewController *registVC = [[RegistViewController alloc]init];
    [self.navigationController pushViewController:registVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//登录按钮
- (IBAction)LoginBtnAction:(id)sender {
    [NetWorkRequestManager requestUrl:[NSURL URLWithString:@"http://api2.pianke.me/user/login"] RequestMethod:POST Params:@{@"email":self.userNameTextField.text,@"passwd":self.passwordTextField.text} Result:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (data) {
            NSMutableDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSString *msg = jsonDict[@"data"][@"msg"];
            msg = msg?msg:@"登录成功";
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:msg  preferredStyle:UIAlertControllerStyleAlert];
            [alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                if ([jsonDict[@"result"] isEqual:@1]) {
//                    ToutiaoTableViewController *touVC = [[ToutiaoTableViewController alloc]init];
//                    [self.navigationController pushViewController:touVC animated:YES];
                    [self.navigationController popViewControllerAnimated:YES];
                }
            }]];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self presentViewController:alertC animated:YES completion:nil];
            });
            if ([jsonDict[@"result"] isEqual:@1]) {
                [[UserInfoManager shareManager]loginWithUserDict:jsonDict[@"data"]];
            }
        }
    }];
//
//    [NetWorkRequestManager requestUrl:[NSURL URLWithString:@"http://api2.pianke.me/user/login"] RequestMethod:POST Params:@{@"email":self.userNameTextField.text,@"passwd":self.passwordTextField.text} Result:^(NSData *data, NSURLResponse *response, NSError *error) {
//        if (data) {
//            NSMutableDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:jsonDict[@"data"][@"msg"] preferredStyle:UIAlertControllerStyleAlert];
//            [alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//                if ([jsonDict[@"result"]isEqual:@1]) {
//                    [self dismissViewControllerAnimated:YES completion:^{
//                        
//                    }];
//                }
//            }]];
//            dispatch_async(dispatch_get_main_queue(), ^{
//                [self presentViewController:alertC animated:YES completion:nil];
//            });
//            if ([jsonDict[@"result"] isEqual:@1]) {
//                [[UserInfoManager shareManager]loginWithUserDict:jsonDict[@"data"]];
//                
//            }
//        }
//    }];

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
